cmake_minimum_required(VERSION 3.21)
project(edlbuild LANGUAGES C CXX VERSION 0.0.1)
set (CMAKE_USE_RELATIVE_PATHS ON)

find_package(LLVM)
find_package(FLEX)
find_package(BISON)


include_directories(
    .
    common/inc
    ${CMAKE_CURRENT_BINARY_DIR}/front-end
    ${LLVM_INCLUDE_DIRS}
)

add_definitions(-D__OPT_DEBUG)

llvm_map_components_to_libnames(llvm_libs support core irreader bitwriter)

flex_target(SCANNER
    front-end/scanner.lpp
    ${CMAKE_CURRENT_BINARY_DIR}/front-end/scanner.cpp
    DEFINES_FILE ${CMAKE_CURRENT_BINARY_DIR}/front-end/scanner.h
    COMPILE_FLAGS "--noline"
)

bison_target(GRAMMAR
    front-end/grammar.ypp
    ${CMAKE_CURRENT_BINARY_DIR}/front-end/grammar.cpp
    DEFINES_FILE ${CMAKE_CURRENT_BINARY_DIR}/front-end/grammar.h
    COMPILE_FLAGS "--debug --no-lines --token-table -d -Wcex"
)

add_flex_bison_dependency(SCANNER GRAMMAR)

add_executable(${PROJECT_NAME}
    main.cpp
    common/src/ast.cpp
    common/src/astDebug.cpp
    common/src/ErrInfo.cpp
    common/src/functionTable.cpp
    common/src/symTable.cpp
    common/src/variableTable.cpp
    back-end/src/codeGen.cpp
    ${FLEX_SCANNER_OUTPUTS}
    ${BISON_GRAMMAR_OUTPUTS}
)

target_link_libraries(${PROJECT_NAME} ${llvm_libs})
